package com.next.servlet;

import com.bean.User;
import com.dao.UserDao;
import com.utils.MD5Util;
import com.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName LoginServlet
 * @Desc 后台登录后台控制层
 * @Author xiaoding
 * @Date 2021/1/13 20:45
 * @Version 1.0
 */
@WebServlet(name = "NextLoginServlet",value = "/NextLoginServlet")
public class LoginServlet extends HttpServlet {
    private UserDao userDao = new UserDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        if (str.equals("login")) {
            //获取用户名、密码
            String name = request.getParameter("name");
            String pws = request.getParameter("pws");

            //判断用户名、密码是否正确
            User user = userDao.LoginFlag(new User(name, MD5Util.MD5(pws)));

            //定义状态、路径
            String message = null;
            String url = null;

            //判断用户是否为空
            if (user != null) {
                //获取验证码、系统验证码
                String yzm = request.getParameter("yzm");
                String yzm2 = (String) request.getSession().getAttribute("checkcode");

                //判断验证码是否相同
                if (yzm.equals(yzm2)) {
                    message = "登录成功";
                    url = "NextBookServlet?flag=select";

                    //设置为加密前的密码
                    user.setPws(pws);
                    //将用户保存到浏览器中和Cookie中
                    request.getSession().setAttribute("user",user);
                    //创建cookie将用户名和密码存储
                    Cookie cookie = new Cookie("login", name + ":" + pws);
                    cookie.setPath("/");
                    cookie.setMaxAge(24*60*60*1000);
                    response.addCookie(cookie);

                }else {
                    message = "验证码不正确";
                    url = "login.jsp";
                }
            }else {
                message = "用户名或密码不正确";
                url = "login.jsp";
            }
            //调用跳转的方法
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("exit")) {
            //设置回话
            request.getSession().setAttribute("user",null);

            //清除cookie存储用户和密码
            Cookie cookie = new Cookie("login", null);
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);

            //跳转
            ResponseUtils.writeMessage(response, "退出成功", "login.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
